package org.shangrila.app.wfm.mb;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.diagram.DefaultDiagramModel;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.comm.helper.StringHelper;
import org.shangrila.wfm.core.WfmEngine;
import org.shangrila.wfm.core.WfmHelper;
import org.shangrila.wfm.eo.WfmTaskLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@RequestScoped
public class WfmInfoMgrMB implements Serializable{
	private static final Logger log = LoggerFactory.getLogger(WfmInfoMgrMB.class);
	private static final long serialVersionUID = 4349454492581896952L;

	private String operator;  
	private String taskid;  
	private List voList; 
	private WfmEngine engine;	
	private WfmTaskLog selectedTask;
	private DefaultDiagramModel model;

	public WfmInfoMgrMB() {
		engine = WfmHelper.getEngine();
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="wfm";
		}
		queryTaskList();
	}
	
	//根据当前用户查询流程实例列表
	public String queryTaskList() {
		if(StringHelper.isEmpty(taskid))
			voList = engine.getWfmdao().findTaskLogInfo(operator, SecUtil.getCachedShiroRoles());
		else
			voList = engine.getWfmdao().findTaskLogNoInfo(taskid,operator, SecUtil.getCachedShiroRoles());
		return null;
	}
	
	//处理行选择
   public void onTodoRowSelect(SelectEvent event) {
	    selectedTask = (WfmTaskLog) event.getObject();	  
	    if(null!=selectedTask) {
		    this.model = WfmDiagramModel.genProcessDiagramModel(selectedTask.getWfmProcess(),selectedTask.getTaskName()); //获取流程图模型
		    //model.findElement(selectedTask.getTaskName()).setStyleClass("ui-diagram-focus");//设置当前节点的特殊颜色	    
	    }
   }
 
    public void onTodoRowUnselect(UnselectEvent event) {
    }
    
 	//show faces Messages
	public void showMessages(String summary,String detail) {	
		FacesMessage message = new FacesMessage(summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getTaskid() {
		return taskid;
	}

	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	public List getVoList() {
		return voList;
	}

	public void setVoList(List voList) {
		this.voList = voList;
	}

	public WfmTaskLog getSelectedTask() {
		return selectedTask;
	}

	public void setSelectedTask(WfmTaskLog selectedTask) {
		this.selectedTask = selectedTask;
	}

	public DefaultDiagramModel getModel() {
		return model;
	}

	public void setModel(DefaultDiagramModel model) {
		this.model = model;
	}
	
}
